package com.wf.it.hpplan.service.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.wf.it.common.api.BookService;
import com.wf.it.common.entity.Book;
import com.wf.it.common.entity.Plan;
import com.wf.it.common.vo.Result;
import com.wf.it.hpplan.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao dao;

    @Override
    @LcnTransaction
    @Transactional
    public Result<Book> addBook(int planid, String userIdCard,int money) {
        Book b=new Book();
        b.setBookTime(new Date());
        b.setState(1);
        b.setUserIdCard(userIdCard);
        b.setPlan(new Plan(planid));
        b.setMoney((double) money);
        dao.save(b);
        return new Result<>();
    }

    @Override
    public Result<List<Book>> findListByUser(String userIdCard) {
        List<Book> list= dao.findByUserIdCard(userIdCard);
        return new Result<List<Book>>(list);
    }

    @Override
    public Result<Book> findById(int bookid) {
        Book book=dao.findById(bookid).get();
        return new Result<>(book);
    }

    @LcnTransaction
    @Transactional
    @Override
    public Result cancelBook(int bookid) {
        dao.updateBookState(bookid,2);
        return Result.success();
    }
}
